package com.sinog.front.controller.biz;

import com.sinog.core.model.UserDTO;
import com.sinog.core.util.ConvertHttpToMap;
import com.sinog.core.utilbean.Constants;
import com.sinog.front.feign.biz.HomeStatisticalService;
import com.sinog.front.feign.biz.census.AllCensusService;
import com.sinog.front.service.user.UserFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @desc 首页
 * @author -
 * @date -
 */
@Controller
public class HomeStatisticalController {

    @Autowired
    private UserFacade userFacade;
    @Resource
    private HomeStatisticalService homeStatisticalService;
    @Autowired
    private AllCensusService allCensusService;

    /**
     * 点击首页通知栏数据改变状态
     * @param request request
     * @return Object
     */
    @RequestMapping(value = "/main/changeState", method = RequestMethod.POST)
    @ResponseBody
    public Object changeState(HttpServletRequest request) {
        UserDTO authentication = userFacade.getAuthentication();
        String account = authentication.getAccount();
        Map<String,Object> searchMap = ConvertHttpToMap.getParameterMap(request);
        String contentId = (String)searchMap.get("id");
        String type = (String)searchMap.get("type");
        Map<String,Object> resmap = new HashMap<>(16);
        List<Map<String,Object>> returnList = new ArrayList<>(16);
        Map<String,Object> changeMap = new HashMap<>(16);
        //全部帮教对象数量
        Integer changeNum = allCensusService.changeState(contentId,type,account);
        changeMap.put("name","changeNum");
        changeMap.put("value",changeNum);
        returnList.add(changeMap);
        resmap.put("error",Constants.Number.NUM_ZERO);
        resmap.put("data",returnList);
        resmap.put("msg","success");
        return resmap;
    }

    /**
     * 首页左侧 机构人员信息
     * @return Object
     */
    @GetMapping("/main/selectJgryxx")
    @ResponseBody
    public Object selectJgryxx() {
        UserDTO authentication = userFacade.getAuthentication();
        String departId = authentication.getDepartid();
        String level = authentication.getLevel();
        //获取当前登录用户的部门名称显示
        String departName = authentication.getDepartName();
        List<Map<String,Object>> returnList = new ArrayList<>(16);
        Map<String,Object> resmap = new HashMap<>(16);
        //主管领导
        List<HashMap<String,Object>> zgldList = homeStatisticalService.zgldList(departId,level);
        //工作人员
        List<HashMap<String,Object>> gzryList = homeStatisticalService.gzryList(departId,level);
        for(HashMap<String,Object> objectHashMap : zgldList) {
            Map<String,Object> zgldMap = new HashMap<>(16);
            //职务
            zgldMap.put("duty",objectHashMap.get("duty"));
            //姓名
            zgldMap.put("name",objectHashMap.get("name"));
            //联系电话
            zgldMap.put("phone",objectHashMap.get("phone"));
            returnList.add(zgldMap);
        }
        for(HashMap<String,Object> stringObjectHashMap : gzryList) {
            Map<String,Object> gzryMap = new HashMap<>(16);
            //职务
            gzryMap.put("duty",stringObjectHashMap.get("duty"));
            //姓名
            gzryMap.put("name",stringObjectHashMap.get("name"));
            //联系电话
            gzryMap.put("phone",stringObjectHashMap.get("phone"));
            returnList.add(gzryMap);
        }
        resmap.put("error",Constants.Number.NUM_ZERO);
        resmap.put("department",departName);
        resmap.put("data",returnList);
        resmap.put("msg","success");
        return resmap;
    }
}